package com.itbaizhan.controller;

import com.itbaizhan.result.BaseResult;
import com.itbaizhan.result.CodeEnum;
import com.itbaizhan.utils.JWTUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author spf
 * @date 2024/2/13
 * @time 15:18
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @PostMapping("/login")
    public BaseResult login(@RequestParam("username") String username, @RequestParam("password") String password){
        /// TODO 数据库操作
        if ("admin".equals(username) && "admin".equals(password)){
            String token = JWTUtils.token(username);
            return BaseResult.ok(token);
        }else {
//            return new BusException(CodeEnum.TOKEN_ERROR);
            return new BaseResult(CodeEnum.TOKEN_ERROR.getCode(), CodeEnum.TOKEN_ERROR.getMessage(), null);
        }
    }

    @GetMapping("/verify")
    public BaseResult verify(@RequestParam("token") String token){
        Boolean verify = JWTUtils.verify(token);
        if (verify){
            return BaseResult.ok();
        }else {
            return new BaseResult(501, "token失效", null);
        }
    }

    @GetMapping("/test")
    public String test(String a){
        return a;
    }
}
